﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;


namespace SGSN
{
    public class Read
    {
        public string getClearHexCode(string hexCode)
        {
            string result = "";
            for (int i = 0; i < hexCode.Length; i++)
            {
                if (hexCode[i] != '-')
                {
                    result = result + hexCode[i];
                }
            }
            return result;
        }

        public string getSwapedInfo(string hexCode)
        {
            string result = null;

            for (int i = 0; i < hexCode.Length; i = i + 2)
            {
                if (hexCode[i] != 'F')
                {
                    result = result + hexCode[i + 1] + hexCode[i];
                }
                else if (hexCode[i] == 'F')
                {
                    result = result + hexCode[i + 1];
                }
            }
            return result;
        }

        public string HexString2Ascii(string hexString)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i <= hexString.Length - 2; i += 2)
            {
                sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber))));
            }
            return sb.ToString();
        }

        public  string Readf(BinaryReader BR, int lenght, int loop, string fieldName)
        {
            string res;
            switch (fieldName)
            {
                case "recordLength":
                    res = "recordLength_ " + BitConverter.ToUInt16(BR.ReadBytes(6), 4).ToString(); break;
                case "IMSI":
                    res = "IMSI_ " + getSwapedInfo(getClearHexCode(BitConverter.ToString(BR.ReadBytes(16), (8)))); break;
                case "IMEI":
                    res = "IMEI_ " + getSwapedInfo(getClearHexCode(BitConverter.ToString(BR.ReadBytes(24), (16)))); break;
                case "MSISDN":
                    res = "MSISDN_ " + getSwapedInfo(getClearHexCode(BitConverter.ToString(BR.ReadBytes(32), (26)))); break;
                case "sGSNAddress":
                    res =  "sGSNAddress_ " + Convert.ToInt32((BitConverter.ToString(BR.ReadBytes(48), (47))), 16) + "."
                + Convert.ToInt32((BitConverter.ToString(BR.ReadBytes(1), (0))), 16) + "."
                + Convert.ToInt32((BitConverter.ToString(BR.ReadBytes(1), (0))), 16) + "."
                + Convert.ToInt32((BitConverter.ToString(BR.ReadBytes(1), (0))), 16); break;
                case "LAC":
                    res = "LAC_ " + BitConverter.ToUInt16(BR.ReadBytes(60), 58).ToString(); break;
                case "gGSNAddressused":
                    res = "gGSNAddressused_ " + Convert.ToInt32((BitConverter.ToString(BR.ReadBytes(80), (79))), 16) + "."
               + Convert.ToInt32((BitConverter.ToString(BR.ReadBytes(1), (0))), 16) + "."
               + Convert.ToInt32((BitConverter.ToString(BR.ReadBytes(1), (0))), 16) + "."
               + Convert.ToInt32((BitConverter.ToString(BR.ReadBytes(1), (0))), 16); break;
                case "apnNetwork":
                    res= "apnNetwork_ " + HexString2Ascii(getClearHexCode(BitConverter.ToString(BR.ReadBytes(147), (83)))); break;
                case "apnOperator":
                    res = "apnOperator_ " + HexString2Ascii(getClearHexCode(BitConverter.ToString(BR.ReadBytes(185), (147)))); break;
                case "servedPDPAddress":
                    res = "servedPDPAddress_ " + Convert.ToInt32((BitConverter.ToString(BR.ReadBytes(200), (199))), 16) + "."
               + Convert.ToInt32((BitConverter.ToString(BR.ReadBytes(1), (0))), 16) + "."
               + Convert.ToInt32((BitConverter.ToString(BR.ReadBytes(1), (0))), 16) + "."
               + Convert.ToInt32((BitConverter.ToString(BR.ReadBytes(1), (0))), 16); break;
                case "changeTime":
                    res = "changeTime_ " + "20" + BitConverter.ToString(BR.ReadBytes(206), 205) + "."
                + BitConverter.ToString(BR.ReadBytes(1), 0) + "." + BitConverter.ToString(BR.ReadBytes(1), 0) + " " //Date
                + BitConverter.ToString(BR.ReadBytes(1), 0) + ":" + BitConverter.ToString(BR.ReadBytes(1), 0) + ":"
                + BitConverter.ToString(BR.ReadBytes(1), 0); break;
                case "changeTimeZone":
                    res = "changeTimeZone_ " + HexString2Ascii(getClearHexCode(BitConverter.ToString(BR.ReadBytes(212), (211)))) + getClearHexCode(BitConverter.ToString(BR.ReadBytes(2), 0)); break;
                case "dataVolumeGPRSUplink":
                    res = "dataVolumeGPRSUplink_ " + BitConverter.ToUInt16(BR.ReadBytes(218), 214); break;
                case "dataVolumeGPRSDownlink":
                    res = "dataVolumeGPRSDownlink_ " + BitConverter.ToUInt16(BR.ReadBytes(222), 218); break;
                 case "qosRequested":
                     res = "qosRequested_ " + getClearHexCode(BitConverter.ToString(BR.ReadBytes(235), 222));; break;
                 case "qosNegotiated":
                     res = "qosNegotiated_ " + getClearHexCode(BitConverter.ToString(BR.ReadBytes(248), 235));; break;
                 case "locationAreaCode":
                     res = "locationAreaCode_ " + getSwapedInfo(getClearHexCode(BitConverter.ToString(BR.ReadBytes(250), 248))); break;
                 case "routingArea":
                     res = "routingArea_ " + getClearHexCode(BitConverter.ToString(BR.ReadBytes(251), 250)); break;
                 case "cellId":
                     res = "cellId_ " + getSwapedInfo(getClearHexCode(BitConverter.ToString(BR.ReadBytes(253), 251))); break;
                 case "recordOpeningTime":
                     res = "recordOpeningTime_ " + "20" + getClearHexCode(BitConverter.ToString(BR.ReadBytes(450), 449)) + "." + getClearHexCode(BitConverter.ToString(BR.ReadBytes(1), 0)) + "." + getClearHexCode(BitConverter.ToString(BR.ReadBytes(1), 0)) + " "
                + BitConverter.ToString(BR.ReadBytes(1), 0) + ":" + BitConverter.ToString(BR.ReadBytes(1), 0) + ":" + BitConverter.ToString(BR.ReadBytes(1), 0); break;
                 case "recordOpeningTimeZone":
                     res = "recordOpeningTimeZone_ " + HexString2Ascii(getClearHexCode(BitConverter.ToString(BR.ReadBytes(456), (455)))) + getClearHexCode(BitConverter.ToString(BR.ReadBytes(2), 0)); break;
                 case "duration":
                     res = "duration_ " + BitConverter.ToUInt16(BR.ReadBytes(462), 458).ToString(); break;
                 case "rncUnsentDownlinkVol":
                     res = "rncUnsentDownlinkVol_ " + BitConverter.ToUInt16(BR.ReadBytes(466), 464).ToString(); break;
                 case "sGSNChange":
                     res = "sGSNChange_ " + getClearHexCode(BitConverter.ToString(BR.ReadBytes(467), (466))); break;
                 case "causeforRecordClosing":
                     res = "sGSNChange_ " + getClearHexCode(BitConverter.ToString(BR.ReadBytes(468), (467))); break;
                 case "value":
                     res = "value_ " + (BitConverter.ToString(BR.ReadBytes(469), (468)));break;
                 case "initiationCause":
                     res = "initiationCause_ " + getClearHexCode(BitConverter.ToString(BR.ReadBytes(470), (469))); break;
                 case "processFamilyId":
                     res = "processFamilyId_ " + getSwapedInfo(getClearHexCode(BitConverter.ToString(BR.ReadBytes(472), (470)))); break;
                 case "sgsnErrorCause":
                     res = "sgsnErrorCause_ " + getSwapedInfo(getClearHexCode(BitConverter.ToString(BR.ReadBytes(476), (472)))); break;
                default: res="aa"; break;
            }                                                                                                                       
            return res;           
        }
    }
}
